<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>菜单编辑页面</title>
    <link rel="stylesheet" th:href="@{/pear/component/pear/css/pear.css}"/>
</head>
<body>
<form class="layui-form" method="POST" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="layui-form-item">
                <label class="layui-form-label">菜单名称</label>
                <div class="layui-input-block">
                    <input type="text" name="title" lay-verify="required|title" maxlength="10" autocomplete="off"
                           placeholder="请输入菜单名称"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">菜单描述</label>
                <div class="layui-input-block">
                    <input type="text" name="description" maxlength="30" lay-verify="description" autocomplete="off"
                           placeholder="请输入菜单描述"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">菜单url</label>
                <div class="layui-input-block">
                    <input type="text" name="href" maxlength="50" lay-verify="href" autocomplete="off"
                           placeholder="href"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">菜单排序号</label>
                <div class="layui-input-block">
                    <input type="text" name="menuSort" lay-verify="menuSort" autocomplete="off"
                           placeholder="排序号"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">菜单type</label>
                <div class="layui-input-block">
                    <select name="type" lay-verify="type" class="layui-select">
                        <option value="">请选择菜单类型</option>
                        <option value="0">顶级菜单</option>
                        <option value="1">页面菜单(无子菜单）</option>
                        <option value="2">顶级菜单(有页面）</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item " style="display:flex">
                <div>
                    <label class="layui-form-label">选择父级菜单</label>
                    <div class="layui-input-block">
                        <div id="ID-tree-demo-showCheckbox"></div>
                    </div>
                </div>
                <div>
                    <label class="layui-form-label">选择菜单权限</label>
                    <div class="layui-input-block">
                        <div id="ID-tree-demo-showCheckbox1"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn  layui-btn-sm" lay-submit=""
                    lay-filter="menu-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script th:src="@{/pear/component/layui/layui.js}"></script>
<script th:src="@{/pear/component/pear/pear.js}"></script>
<script th:src="@{/pear/component/layui/authtree.js}"></script>
<script th:inline="javascript">
    let use = layui.use(['form', 'jquery', 'authtree'], function () {
        let baseURL = /*[[@{/}]]*/ '';
        let form = layui.form;
        let layer = layui.layer;
        let $ = layui.$;
        let authtree = layui.authtree;
        let ids = [];
        let checked = parent.permissionList
        // 自定义验证规则
        form.verify({
            title: function (value) {
                if (value.length > 15) {
                    return '菜单名称不能超过15个字符';
                }
            },
            description: function (value) {
                if (value.length > 50) {
                    return '菜单描述不能超过50个字符';
                }
            },
            href: function (value) {
                if (!/^(https?:\/\/|ftp:\/\/|file:\/\/)?[\w\/-]+$/.test(value) && !/^(https?:\/\/|ftp:\/\/|file:\/\/|\/\/)?[\w\.-]+\.\w+$/.test(value)) {
                    return 'URL格式不正确';
                }
            },
            menuSort: function (value) {
                if (!/^\d+$/.test(value)) {
                    return '排序号必须为数字';
                }
            }
        });
        // 渲染
        $.ajax({
            url: baseURL + "menu/list",
            type: 'get',
            success: function (res) {
                let filteredData = res.data.filter(function (item) {
                    return item.menuId !== parent.menuId;
                });
                let trees = authtree.listConvert(filteredData, {
                    primaryKey: 'menuId'
                    , startPid: 0
                    , parentKey: 'parentId'
                    , nameKey: 'title'
                    , valueKey: 'menuId'
                    , checkedKey: res.data.menuId
                });
                // 遍历树数据，设置默认选中的节点
                let parentId = parent.parentId;

                function setDefaultCheckedNodes(nodes, parentId) {
                    nodes.forEach(function (node) {
                        if (node.value === parentId) {
                            node.checked = true; // 设置该节点为选中状态
                        }
                        if (node.list) {
                            setDefaultCheckedNodes(node.list, parentId); // 递归处理子节点
                        }
                    });
                }

                // 调用函数设置默认选中的节点
                setDefaultCheckedNodes(trees, parentId);
                authtree.render('#ID-tree-demo-showCheckbox', trees, {
                    autoWidth: true,
                    openChecked: true,
                    autochecked: false,
                    autoclose: false
                });
                authtree.on('change(#ID-tree-demo-showCheckbox)', function () {
                    // 获取选中节点
                    ids = authtree.getChecked('#ID-tree-demo-showCheckbox');
                });

            }
        })
        $.ajax({
            url: baseURL + "permission/list",
            type: 'get',
            success: function (res) {
                const trees = authtree.listConvert(res.data, {
                    primaryKey: 'permissionId'
                    , startPid: 0
                    , parentKey: 'permissionParentId'
                    , nameKey: 'permissionName'
                    , valueKey: 'permissionId'
                    , checkedKey: res.data.permissionId
                });
                let list = parent.permissionList;

                function setDefaultCheckedNodes(nodes, list) {
                    nodes.forEach(function (node) {
                        if (list.includes(node.value)) {
                            node.checked = true; // 设置该节点为选中状态
                        }
                        if (node.list) {
                            setDefaultCheckedNodes(node.list, list); // 递归处理子节点
                        }
                    });
                }

                // 调用函数设置默认选中的节点
                setDefaultCheckedNodes(trees, list);
                authtree.render('#ID-tree-demo-showCheckbox1', trees, {
                    autoWidth: true,
                    openChecked: true,
                    autochecked: false,
                    autoclose: false
                });
                authtree.showDept('#ID-tree-demo-showCheckbox1', 1);
                authtree.on('change(#ID-tree-demo-showCheckbox1)', function () {
                    // 获取所有已选中节点
                    checked = authtree.getChecked('#ID-tree-demo-showCheckbox1');
                });
            }
        })
        form.on('submit(menu-save)', function (data) {
            data.field.menuId = parent.menuId
            let selectElement = document.querySelector('select[name="type"]').value
            data.field.type = selectElement
            if (ids.length === 0) {
                data.field.parentId = parent.parentId
            } else {
                data.field.parentId = ids[0]
            }
            data.field.permissionIds = checked// 功能权限
            $.ajax({
                url: baseURL + 'menu/update',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.code === 1000) {
                        layer.msg(result.msg, {
                            icon: 1,
                            time: 2000
                        }, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window
                                .name))//关闭当前页
                            parent.render()
                        });
                    } else {
                        layer.msg(result.msg, {
                            icon: 2,
                            time: 2000
                        }, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window
                                .name))//关闭当前页
                        });
                    }
                }
            })
            return false;
        });
    });
</script>
</body>
</html>
